package com.hqd.jjvm.jnative.lang;

import com.hqd.jjvm.jnative.JNativeRegistry;
import com.hqd.jjvm.rtda.JThread;
import com.hqd.jjvm.rtda.StackFrame;

public class JDoubleNativeRegistry extends JNativeRegistry {
    private static final JDoubleNativeRegistry instance = new JDoubleNativeRegistry();
    protected static final String CLASS_PATH = "java/lang/Double";

    static {
        registry(CLASS_PATH, "doubleToRawLongBits", "(D)J", JDoubleNativeRegistry::doubleToRawLongBits);
        registry(CLASS_PATH, "longBitsToDouble", "(J)D", JDoubleNativeRegistry::longBitsToDouble);
    }

    protected static void longBitsToDouble(JThread jThread) {
        StackFrame stackFrame = jThread.getJvmStack().getTop();
        long bits = stackFrame.getLocalVarsIntVal(0);
        stackFrame.getOperandStack().pushDouble(Double.longBitsToDouble(bits));
    }

    protected static void doubleToRawLongBits(JThread jThread) {
        StackFrame stackFrame = jThread.getJvmStack().getTop();
        double val = stackFrame.getLocalVars().getDoubleVal(0);
        stackFrame.getOperandStack().pushLong(Double.doubleToRawLongBits(val));
    }

    public static JDoubleNativeRegistry getInstance() {
        return instance;
    }
}
